package io.renren.modules.wyzs.controller;


import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.wyzs.entity.FactoryInfo;
import io.renren.modules.wyzs.service.FactoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/factory")
public class FactoryController {

    @Autowired
    FactoryService factoryService;


    @RequestMapping("/queryList")
    public R list(@RequestParam Map<String, Object> params)
    {
        //查询列表数据
        Query query = new Query(params);
        int total = factoryService.queryTotal(query);
        int page=Integer.parseInt((String)params.get("page"));

        int limit=Integer.parseInt((String) params.get("limit"));
        int start=(page-1)*limit;
        query.put("start",start);
        List<FactoryInfo> configList = factoryService.queryList(query);

        PageUtils pageUtil = new PageUtils(configList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    @RequestMapping("/getFactoryList")
    public R getFactoryList(@RequestParam Map<String, Object> params)
    {
        List<FactoryInfo> configList = factoryService.getFactoryList(params);

        return R.ok().put("data", configList);
    }

    @RequestMapping("/getFactoryListX")
    public R getFactoryListX(@RequestParam Map<String, Object> params)
    {
        List<FactoryInfo> configList = factoryService.getFactoryListX(params);

        return R.ok().put("data", configList);
    }


    @RequestMapping("/info/{id}")
    public R list(@PathVariable("id") String id)
    {

        FactoryInfo factoryInfo = factoryService.queryInfo(id);
        return R.ok().put("data", factoryInfo);
    }

    /**
     * 修改用户
     */
    @SysLog("修改工厂信息")
    @RequestMapping("/update")
    public R update(@RequestBody FactoryInfo factoryInfo){

        factoryService.update(factoryInfo);
        return R.ok();
    }

    /**
     * 修改用户
     */
    @SysLog("新增工厂")
    @RequestMapping("/insert")
    public R insert(@RequestBody FactoryInfo factoryInfo){
        //生成uuid
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        factoryInfo.setId(uuid);
        factoryService.save(factoryInfo);
        return R.ok();
    }

    /**
     * 修改用户
     */
    @SysLog("删除工厂")
    @RequestMapping("/delete")
    public R delete(@RequestBody String id){
        String ids=id.substring(1,id.length()-1);
        factoryService.delete(ids);
        return R.ok();
    }


}
